package com.example.forum.controller;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request, @NonNull Long articleId,@NonNull String content){
        //获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if (user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取要回复的帖子对象
        Article article = articleService.selectDetailById(articleId);
        if (article == null || article.getDeleteState() == 1 || article.getState() == 1){
            return AppResult.failed(ResultCode.FAILED);
        }
        //构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);//要回复的帖ID
        articleReply.setPostUserId(user.getId());//回复的发送者
        articleReply.setContent(content);//回复的内容
        return AppResult.success();
    }

    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@NonNull Long articleId) {
            // 查询帖⼦是否存在
            Article article = articleService.selectDetailById(articleId);
            if (article == null || article.getDeleteState() == 1) {
            // 记录⽇志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new
                    ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
            }
            // 查询结果
            List<ArticleReply> result =
            articleReplyService.selectByArticleId(articleId);
            // 返回结果
            return AppResult.success(result);
    }

}





















